/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.platform.dao {
	
	import com.adobe.net.URI;
	import com.cred.industries.platform.dao.ServerDAODefines;
	import com.cred.industries.platform.model.LocalUserData;
	
	import flash.events.ErrorEvent;
	import flash.utils.ByteArray;
	
	import org.httpclient.HttpClient;
	import org.httpclient.HttpRequest;
	import org.httpclient.events.HttpDataListener;
	import org.httpclient.events.HttpResponseEvent;
	import org.httpclient.events.HttpStatusEvent;
	import org.httpclient.http.Post;

	public class MetricsServerDAO {
		
		private static var METRICS_PATH:String = "metrics/";
		private static var TIME_IN_APP_PATH:String = "secondsInApp";
		
		private var mStatusCode:int = 0;
		private var mData:ByteArray = null;
		
		public function MetricsServerDAO() { }

		
		public function sendTimeInApp(secondsInApp:int):void {
			
			var sessionKey:String = "";
			
			if(LocalUserData.get().isLoggedIn()) {
				sessionKey = LocalUserData.get().sessionKey;
			}
			
			var client:HttpClient = new HttpClient();
			
			var listener:HttpDataListener = new HttpDataListener();
			listener.onStatus = statusHandler;
			listener.onDataComplete =  dataCompleteHandler;
			listener.onComplete = metricSentHandler;
			listener.onError = errorHandler;
			client.listener = listener;
			
			var uri:URI = new URI(ServerDAODefines.serverURL +  METRICS_PATH + TIME_IN_APP_PATH);
			uri.setQueryValue("secondsInApp", secondsInApp.toString());
			
			var request:HttpRequest = new Post();
			request.addHeader("sessionid", sessionKey);
			
			client.request(uri, request);
		}
		
		private function metricSentHandler(event:HttpResponseEvent):void {
			
			if(mStatusCode == 200 || mStatusCode == 201)
				trace("MetricsServerDAO submitted metric successfully");
			else
				trace("MetricsServerDAO failed submitting metric");
		}
				
		private function errorHandler(event:ErrorEvent):void {
			
			trace("MetricsServerDAO Had problem connecting. " + event.toString() );
		}
		
		private function dataCompleteHandler(event:HttpResponseEvent, data:ByteArray):void {
			
			trace("MetricsServerDAO data " + data.toString());
			mData = data;
		}
		
		private function statusHandler(event:HttpStatusEvent):void {
			
			trace("MetricsServerDAO HTTPStatusEvent " + event.response.code); 
			mStatusCode = int(event.response.code);
		}
	}
}